﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockPrjTeam1.Biz;
using MockPrjTeam1.Entity;

namespace MockPrjTeam1
{
    public partial class Default : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["username"] != null)
            {
                Username1.Text = Session["username"].ToString();
            }
            else
            {

                if ((Request.Cookies["UserInfo"] != null))
                {
                    string username = Request.Cookies["UserInfo"]["Username"];
                    string password = Request.Cookies["UserInfo"]["Password"];
                    Username1.Text = username;
                    if (LoginBiz.IsValidLogin(username, password))
                    {

                        if (Session["username"] == null)
                        {
                            Session.Add("username", "accept");
                        }
                        Session["username"] = username;
                        UserOnline UserOnline = new UserOnline();
                        UserOnline.username = username;

                        HttpCookie myCookie = new HttpCookie("UserInfo");
                        myCookie["Username"] = username;
                        myCookie["Password"] = password;
                        myCookie.Expires = DateTime.Now.AddDays(1d);
                        Response.Cookies.Add(myCookie);

                    }
                }
                else
                {

                    Response.AddHeader("refresh", "0;url=/Pages/Forms/Login.aspx");
                    Username1.Text = "You haven't logged on yet";

                }
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            if (Session["username"] != null)
            {
                Session.Abandon();
            }

            HttpCookie myCookie = new HttpCookie("UserInfo");
            myCookie["Username"] = "";
            myCookie["Password"] = "";
            myCookie.Expires = DateTime.Now.AddDays(-1d);
            Response.Cookies.Add(myCookie);

            Response.AddHeader("refresh", "0;url=/Pages/Forms/Login.aspx");
        }
    }
}